package top.smallway.lightbandassistant_app.UI;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.HashMap;
import java.util.Map;

import top.smallway.lightbandassistant_app.R;
import top.smallway.lightbandassistant_app.network.LoginManager;

/**
 * 主界面Activity
 * 负责管理底部导航栏和三个主要Fragment（首页、发现、我的）的切换显示
 */
public class MainActivity extends AppCompatActivity {
    // 底部导航视图
    private BottomNavigationView bottomNavigationView;
    // 存储所有Fragment的列表
    private Map<Integer, Fragment> fragments = new HashMap<>();
    // 登录管理器实例
    public static LoginManager loginManager;

    /**
     * Activity创建时的初始化
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();      // 初始化视图
        InitFargment();  // 初始化Fragment
        initEvent();     // 初始化事件
    }

    /**
     * 初始化Fragment事件
     * 设置默认显示首页Fragment，并将其他Fragment添加到容器中
     */
    private void initEvent() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 默认显示首页（home Fragment）
        fragmentTransaction.add(R.id.fragment, fragments.get(R.id.home_page), "home");
        fragmentTransaction.add(R.id.fragment, fragments.get(R.id.find_page), "find");
//        fragmentTransaction.add(R.id.fragment, fragments.get(R.id.playlist_page), "playlist");
        fragmentTransaction.add(R.id.fragment, fragments.get(R.id.my_page), "mine");
        fragmentTransaction.show(fragments.get(R.id.home_page)); // 显示第一个 Fragment
        fragmentTransaction.commit();
    }

    /**
     * 初始化所有Fragment
     * 创建并添加首页、发现、我的三个主要Fragment
     */
    private void InitFargment() {
        Mine mine = new Mine();
        Home home = new Home();
        Find find = new Find();
        // PlayList playList = new PlayList();
        fragments.put(R.id.home_page, home);
        fragments.put(R.id.find_page, find);
        fragments.put(R.id.my_page, mine);
//        fragments.put(R.id.playlist_page, discovery);
    }

    /**
     * 初始化视图组件
     * 设置底部导航栏的点击监听，实现Fragment的切换
     */
    private void initView() {
        loginManager = new LoginManager(this);
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId(); // 获取选中的 MenuItem ID
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                for (Integer id : fragments.keySet()) {
                    Fragment fragment = fragments.get(id);
                    if (null != fragment) {
                        if (itemId == id) {
                            fragmentTransaction.show(fragment);
                        } else {
                            fragmentTransaction.hide(fragment);
                        }
                    }
                }

                fragmentTransaction.commit(); // 提交事务
                return true;
            }


        });
    }
}